package stack;

import java.util.Stack;

/**
 * @author Liaorun
 */
public class StackAndQueueConvert {

    /**
     * 用两个栈模拟队列
     */
    public static class TowStacksQueue {
        private Stack<Integer> stackPush;
        private Stack<Integer> stackPop;


        public TowStacksQueue() {
            stackPop = new Stack<>();
            stackPush = new Stack<>();
        }


        public void push(int pushInt) {
            stackPush.push(pushInt);
            dao();
        }

        public int poll() {
            if (stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty");
            }
            dao();

            return stackPop.pop();
        }

        public int peek() {
            if (stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty");
            }
            dao();

            return stackPop.peek();
        }

        private void dao() {
            // pop 栈为空就可以倒数据
            if (stackPop.isEmpty()) {
                // 把 push 栈的数据全部放到 pop栈
                while (stackPush.isEmpty()) {
                    stackPop.push(stackPush.pop());
                }
            }
        }
    }
}
